<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <input id="bookname" type="text">
    <button>添加</button>
    <ul></ul>

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        // const url = 'http://www.liulongbin.top:3006/api/getbooks';
        // axios.get(url);

        const inp = document.querySelector('#bookname');
        const btn = document.querySelector('button');

        const url = 'http://www.liulongbin.top:3006/api/addbook';

        function $post (conf) {
            const xhr = new XMLHttpRequest();
            xhr.open(conf.method, conf.url);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.send(JSON.stringify(conf.data));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    conf.success && conf.success(JSON.parse(xhr.responseText));
                }
            };
        }

        btn.addEventListener('click', () => {

            const data = {
                bookname: inp.value,
                author: 'yuzelin',
                publisher: '人民日报',
            };

            $post({
                method: 'post',
                data,
                url,
                success (res) {
                    console.log(res);
                },
            });

        });



    </script>
</body>
</html>